/* ************************************************************************ */
/* PROGRAM: millermdacdbasecrt.sas                    LAST UPDATED:11/26/18 */ 
/* ------------------------------------------------------------------------ */
/* Program collects the basic variables to create the analysis file for the */
/*  research of Sarah Miller of the University of Michigan and Laura Wherry */
/*  of the University of San Diego, MDAC MS#018.                            */
/*  This study will look at the affect of the Affordable Care Act (ACA) on  */
/*  mortality outcomes.  Only 26 states participated in the ACA and         */
/*  the research will compare the results for the 26 states to the non      */
/*  participating states.                                                   */
/* Basic exclusions are implemented to obtain a core MDAC analysis file.    */
/* ************************************************************************ */
LIBNAME mdaclib '/data/mdac/data/mdac_master_file';
LIBNAME mdaclibr '/data/mdac/library';
LIBNAME datalib '/data/mdac/data/research_file';
LIBNAME caidlib '/data/mdac/data/cms/cms_data';
LIBNAME millrlib '/data/mdac/data/miller';

*filename outpfile '/data/mdac/data/temp/mdacpuf.dat';

OPTION LS=80 fmtsearch=(mdaclibr);

PROC FORMAT;
 value $povpimfmt
              '   ' = 'NIU/Miss'
      '000' - '138' = 'LE138FPL'
      '139' - '399' = '139399FPL'
      '400' - '999' = 'GE400FPL' ;     

 value $povpimfmtx
                '1' = 'LE138FPL'
                '2' = '139399FPL'
                '3' = 'GE400FPL'      
                '9' = 'NIU/Miss' ;

 value follofmt 0 = 'Censored'
                1 = 'dth 2008'
                2 = 'dth 2009'
                3 = 'dth 2010'
                4 = 'dth 2011'
                5 = 'dth 2012'
                6 = 'dth 2013'
                7 = 'dth 2014'
                8 = 'dth 2015' ;

 value follo2fmt 0 = 'Censored'
                 1 = 'dth20081'
                 2 = 'dth20082'
                 3 = 'dth20091'
                 4 = 'dth20092'
                 5 = 'dth20101'
                 6 = 'dth20102'
                 7 = 'dth20111'
                 8 = 'dth20112'
                 9 = 'dth20121'
                10 = 'dth20122'
                11 = 'dth20131'
                12 = 'dth20132'
                13 = 'dth20141'
                14 = 'dth20142'
                15 = 'dth20151'
                16 = 'dth20152'  ;

 value age14fmt 0 - 18 = 'Age 0-18'
               19 - 49 = 'Age19-49'
               50 - 64 = 'Age50-64'
              65 - 200 = 'Age 65+ ' ;
run;
* *************************************************************** ;
* xxx COLLECT DATA FROM THE ACS MASTERFILES                       ;
* *************************************************************** ;
/* 
proc contents data=mdaclib.acs08_pvsmaster_d1 varnum;
*proc contents data=mdaclib.acs08_pvsmaster_d1;
title1 '****************************************************************';
title2 '*  MDAC FILE:mdaclib.acs08_pvsmaster_d1                        *';
title3 '****************************************************************';
run;
*endsas;
proc contents data=mdaclib.acs08_pvsmaster_d2 varnum;
*proc contents data=mdaclib.acs08_pvsmaster_d2 ;
title1 '****************************************************************';
title2 '*  MDAC FILE:mdaclib.acs08_pvsmaster_d2 *';
title3 '****************************************************************';
run;
endsas;
*/
data mdacmastrdata(keep=mdacid ssiposinc );
 length match $6 stcounty $5 sttract $8 trctytr $14;
* set mdaclib.acs08_pvsmaster_d1 
     mdaclib.acs08_pvsmaster_d2 ;
*  set mdaclib.all_person;
  set mdaclib.mdac_master;
stcounty = st||cty ;
stctytr = stcounty||tr ;
 if ssi > 0 then ssiposinc = 1 ; else ssiposinc = 0;
wgt = 1.0 ;
run;
proc print data=mdacmastrdata (obs = 15);
title1 '*******************************************';
title2 '* xxx PROC PRINT data= mdacmastrdata      *';
title3 '*******************************************';
* var mdacid ssi hupac ;
* var mdacid ssi paoc r18 ssiposinc;
 var mdacid ssiposinc;
 run;

proc freq data=mdacmastrdata ;
title1 '*******************************************';
title2 '* xxx PROC FREQ data= mdacmastrdata       *';
title3 '*******************************************';
 table ssiposinc ;
 run;

proc sort data=mdacmastrdata ; by mdacid ; run;
*endsas;
/* ***************************************************** */
/* xxx INPUT THE BASIC MDAC ANALYSIS DATABASE            */
/* ***************************************************** */
data tmillermdac;
set datalib.analysis_file_v1;
/* ***************************************************** */
/* xxx DEFINE PUBLIC-USE FILE DATABASE EXCLUSIONS        */
/* ***************************************************** */
 if '2' <= matchstat <= '3'; * EXCLUDE FAIL EDITS & EOSF        ; 
 if '1' <= sex <= '2'  ;     * EXCLUDE NON IDENTIFIABLE GENDER  ; 
 if   0 <= age <= 200 ;      * EXCLUDE RECORDS WITH MISSING AGE ; 
/* *********************************************************** */
/* xxx DEFINE NEW VARIABLE DEFINITIONS                         */
/* ********************************************************************** */
/* ********************************************************************** */
/* xxx DEFINE NEW VARIABLE: milpovct - Miller poverty threshold at 138FPL */
/*        and associated indicator variables                              */
/* ********************************************************************** */
 milpovct = '9' ;
  if '000' <= povpi <= '138' then milpovct = '1' ;
  if '138' <  povpi <= '399' then milpovct = '2' ;
  if '399' <  povpi <= '999' then milpovct = '3' ;

  if milpovct = '1' then indpvle138 = 1 ; else indpvle138 = 0;
  if milpovct = '2' then indpv139399 = 1 ; else indpv139399 = 0;
  if milpovct = '3' then indpvge400 = 1 ; else indpvge400 = 0;
  if milpovct = '9' then indpvmiss = 1 ; else indpvmiss = 0;

monthod = month(dod);
dayod = day(dod);
yearod = year(dod);

foloprd = 25;
 if matchstat = '3' then foloprd = 0;
 if matchstat = '2' and yearod = 2008 then foloprd = 1;
 if matchstat = '2' and yearod = 2009 then foloprd = 2;
 if matchstat = '2' and yearod = 2010 then foloprd = 3;
 if matchstat = '2' and yearod = 2011 then foloprd = 4;
 if matchstat = '2' and yearod = 2012 then foloprd = 5;
 if matchstat = '2' and yearod = 2013 then foloprd = 6;
 if matchstat = '2' and yearod = 2014 then foloprd = 7;
 if matchstat = '2' and yearod = 2015 then foloprd = 8;
 if hicov = '2' then indunins = 1; else indunins = 0;
/* *********************************************************** */
/* xxx DEFINE HEALTHCARE AMENABLE CAUSES OF DEATH              */
/* *********************************************************** */
 indhcamen = 0;
 if substr(cause3,1,1) = 'Q' then indhcamen = 1;
 if cause3  in 
     ('A15' 'A16' 'A17' 'A18' 'A19' 'B90' 'A38' 'A39' 'A40' 'A41'
      'A46' 'B50' 'B54' 'G00' 'G03' 'J02' 'L03' 'B20' 'B21'
      'B23' 'B24' 'C18' 'C19' 'C20' 'C21' 'C43' 'C50' 'C53' 'C67'
      'C73' 'C81' 'C91' 'D10' 'D11' 'D12' 'D13' 'D14' 'D15' 'D16' 
          'D17' 'D18' 'D19' 'D20' 'D21' 'D22' 'D23' 'D24' 'D25' 'D26' 
      'D27' 'D28' 'D29' 'D30' 'D31' 'D32' 'D33' 'D34' 'D35' 'D36'
      'E10' 'E11' 'E12' 'E13' 'E14' 'G40' 'G41' 'I01' 'I02' 'I03' 
      'I04' 'I05' 'I06' 'I07' 'I08' 'I09' 'I10' 'I11' 'I12' 'I13'
      'I14' 'I15' 'I20' 'I21' 'I22' 'I23' 'I24' 'I25' 'I60' 'I61' 'I62'
      'I63' 'I64' 'I65' 'I66' 'I67' 'I68' 'I69' 'J09' 'J10' 'J11' 'J12'
      'J13' 'J14' 'J15' 'J16' 'J17' 'J18' 'J45' 'J46' 'K25' 'K26' 'K27'
      'K28' 'K35' 'K36' 'K37' 'K38' 'K40' 'K41' 'K42' 'K43' 'K44' 'K45'
      'K46' 'K80' 'K81' 'K82' 'K83' 'K85' 'N00' 'N01' 'N02' 'N03' 'N04' 
      'N05' 'N06' 'N07' 'N21' 'N35' 'N40' 'Y60' 'Y61' 'Y62' 'Y63' 'Y64'
      'Y65' 'Y66' 'Y67' 'Y68' 'Y69' 'Y83' 'Y84') 
 then indhcamen = 1;
if ucause in 
('A481' 'B171' 'B182' 'C920' 'K861' 'K862' 'K863' 'K864' 'K865' 'K866'
      'K867' 'K868' 'K869' 'K915' 'N991') 
          then indhcamen=1;

indinternal = 0;

 if substr(cause3,1,1) = 'S' then indinternal = 0;
 if substr(cause3,1,1) = 'T' then indinternal = 0;
 if substr(cause3,1,1) = 'V' then indinternal = 0;
 if substr(cause3,1,1) = 'W' then indinternal = 0;
 if substr(cause3,1,1) = 'X' then indinternal = 0;
 if substr(cause3,1,1) = 'Y' then indinternal = 0;
 if substr(cause3,1,1) = 'Z' then indinternal = 0;

 indexternal = 0;
 if substr(cause3,1,1) = 'S' then indexternal = 1;
 if substr(cause3,1,1) = 'T' then indexternal = 1;
 if substr(cause3,1,1) = 'V' then indexternal = 1;
 if substr(cause3,1,1) = 'W' then indexternal = 1;
 if substr(cause3,1,1) = 'X' then indexternal = 1;
 if substr(cause3,1,1) = 'Y' then indexternal = 1;
 if substr(cause3,1,1) = 'Z' then indexternal = 1;

/* *********************************************************** */
/* xxx DEFINE NEW VARIABLE: age_2014 - Age on 1/1/2014         */
/* *********************************************************** */
agedays = mdy(1,1,2014) - mdy(dbm,dbd,dby) ;
age_2014 = floor((mdy(1,1,2014) - mdy(dbm,dbd,dby))/365.25) ;

/* *************************************************************** */
/* xxx DEFINE NEW VARIABLES FOR EXPANSION STATUS ON 1/2 YR PERIODS */
/* *************************************************************** */
 if st in ('010' '011' '025' '036' '050') then indearlyexp = 1;
                                          else indearlyexp = 0;

 if st in ('004' '005' '006' '008' '009' '010' '011' '015' '017' '019'
           '021' '024' '025' '026' '027' '032' '034' '035' '036' '038'
           '039' '041' '044' '050' '053' '054') then indtreated14a = 1;
                                                else indtreated14a = 0;

 if st in ('033') then indtreated14b = 1; else indtreated14b = 0;

 if st in ('018' '042') then indtreated15a = 1; else indtreated15a = 0;

 if st in ('002') then indtreated15b = 1; else indtreated15b = 0;

ambdiff = 0 ;
 if dphy = '1' then ambdiff = 1 ;
 if dphy = ' ' then ambdiff = . ;

treatedgroup = 0;
if indtreated14a = 1 then treatedgroup = 1;
if indtreated14b = 1 then treatedgroup = 2;
if indtreated15a = 1 then treatedgroup = 3;
if indtreated15b = 1 then treatedgroup = 4;

run;
proc sort data=tmillermdac; by st ; run;
/* ********************************************* */
/* xxx VARIABLE REVIEW SECTION                   */
/* ********************************************* */
/*
proc print data=tmillermdac (obs=35); 
title1 '*********************************************************';
title2 '* PRINT: print of file tmillermdac                      *';
title3 '*********************************************************';
*var st pob pobfc stpob start rdate matchstat addyrs followdays
     followyrs censor indstpobagree;
*var st pob pobfc rdate matchstat ;
*var mdacid matchstat mdac_hhid foloprd foloprd2 monthod dayod yearod;
 var age dbd dbm dby agedays age_2014 ;
run;
endsas;

